home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 1.iso
/
toolbox
/
src
/
tutorials
/
custEducation
/
opengl2
/
lib
/
rgbReadImageFile.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-11-11
|
8KB
|
254 lines
/*
* Copyright 1994, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*/
#include <unistd.h>
#include <GL/gl.h>
#include <gl/image.h>
#include "rgbImageFile.h"
/*
* private function prototypes
*
*/
void bwtorgba( register unsigned short *b, register unsigned int *l,
register int n, float alpha );
void rgbtorgba( register unsigned short *r, register unsigned short *g,
register unsigned short *b, register unsigned int *l,
register int n, float alpha );
void rgbtorgba( register unsigned short *r, register unsigned short *g,
register unsigned short *b, register unsigned int *l,
register int n, float alpha );
void rgbatorgba( register unsigned short *r, register unsigned short *g,
register unsigned short *b, register unsigned short *a,
register unsigned int *l, register int n, float alpha );
GLuint *rgbReadImageFile( char *name, GLint *width, GLint *height )
{
unsigned int *base, *lptr;
unsigned short *rbuf, *gbuf, *bbuf, *abuf;
IMAGE *image;
int y;
/* This program and librgbImageFile both rely on an unsigned
* int being 4 unsigned contiguous bytes. The following test
* checks to see if this is the case.
*/
if ( sizeof( unsigned int ) != 4 )
{
fprintf (stderr, "Warning: sizeof( unsigned int ) != 4,\
this program and librgbImageFile must be modified\n"
);
exit (1);
}
image = iopen(name,"r");
if(!image) {
fprintf(stderr,"rgbImageFileRead: can't open image file %s\n",name);
exit(1);
}
*width = image->xsize;
*height = image->ysize;
base = (unsigned int *)malloc(image->xsize*image->ysize*sizeof(unsigned int));
rbuf = (unsigned short *)malloc(image->xsize*sizeof(short));
gbuf = (unsigned short *)malloc(image->xsize*sizeof(short));
bbuf = (unsigned short *)malloc(image->xsize*sizeof(short));
abuf = (unsigned short *)malloc(image->xsize*sizeof(short));
if(!base || !rbuf || !gbuf || !bbuf) {
fprintf(stderr,"rgbImageFileRead: can't malloc enough memory\n");
exit(1);
}
lptr = base;
for(y=0; y<image->ysize; y++) {
if(image->zsize>=4) {
getrow(image,rbuf,y,0);
getrow(image,gbuf,y,1);
getrow(image,bbuf,y,2);
getrow(image,abuf,y,3);
rgbatorgba(rbuf,gbuf,bbuf,abuf,lptr,image->xsize, 1.0);
lptr += image->xsize;
} else if(image->zsize==3) {
getrow(image,rbuf,y,0);
getrow(image,gbuf,y,1);
getrow(image,bbuf,y,2);
rgbtorgba(rbuf,gbuf,bbuf,lptr,image->xsize, 1.0);
lptr += image->xsize;
} else {
getrow(image,rbuf,y,0);
bwtorgba(rbuf,lptr,image->xsize, 1.0);
lptr += image->xsize;
}
}
iclose(image);
free(rbuf);
free(gbuf);
free(bbuf);
free(abuf);
return base;
}
GLuint *rgbReadImageFileAlpha( char *name, GLint *width, GLint *height,
GLfloat alpha)
{
unsigned int *base, *lptr;
unsigned short *rbuf, *gbuf, *bbuf, *abuf;
IMAGE *image;
int y;
/* This program and librgbImageFile both rely on an unsigned
* int being 4 unsigned contiguous bytes. The following test
* checks to see if this is the case.
*/
if ( sizeof( unsigned int ) != 4 )
{
fprintf (stderr, "Warning: sizeof( unsigned int ) != 4,\
this program and librgbImageFile must be modified\n"
);
exit (1);
}
image = iopen(name,"r");
if(!image) {
fprintf(stderr,"rgbImageFileRead: can't open image file %s\n",name);
exit(1);
}
*width = image->xsize;
*height = image->ysize;
base = (unsigned int *)malloc(image->xsize*image->ysize*sizeof(unsigned int));
rbuf = (unsigned short *)malloc(image->xsize*sizeof(short));
gbuf = (unsigned short *)malloc(image->xsize*sizeof(short));
bbuf = (unsigned short *)malloc(image->xsize*sizeof(short));
abuf = (unsigned short *)malloc(image->xsize*sizeof(short));
if(!base || !rbuf || !gbuf || !bbuf) {
fprintf(stderr,"rgbImageFileRead: can't malloc enough memory\n");
exit(1);
}
lptr = base;
for(y=0; y<image->ysize; y++) {
if(image->zsize>=4) {
getrow(image,rbuf,y,0);
getrow(image,gbuf,y,1);
getrow(image,bbuf,y,2);
getrow(image,abuf,y,3);
rgbatorgba(rbuf,gbuf,bbuf,abuf,lptr,image->xsize, alpha);
lptr += image->xsize;
} else if(image->zsize==3) {
getrow(image,rbuf,y,0);
getrow(image,gbuf,y,1);
getrow(image,bbuf,y,2);
rgbtorgba(rbuf,gbuf,bbuf,lptr,image->xsize, alpha);
lptr += image->xsize;
} else {
getrow(image,rbuf,y,0);
bwtorgba(rbuf,lptr,image->xsize, alpha);
lptr += image->xsize;
}
}
iclose(image);
free(rbuf);
free(gbuf);
free(bbuf);
free(abuf);
return base;
}
void
bwtorgba( register unsigned short *b, register unsigned int *l,
register int n, float alpha )
{
register unsigned short scaledAlpha;
scaledAlpha = (unsigned short) (alpha * 0xFF);
/* added FF for alpha so transparent textures will work - mars */
while(n>=8) {
l[0] = scaledAlpha | 0x01010100*b[0];
l[1] = scaledAlpha | 0x01010100*b[1];
l[2] = scaledAlpha | 0x01010100*b[2];
l[3] = scaledAlpha | 0x01010100*b[3];
l[4] = scaledAlpha | 0x01010100*b[4];
l[5] = scaledAlpha | 0x01010100*b[5];
l[6] = scaledAlpha | 0x01010100*b[6];
l[7] = scaledAlpha | 0x01010100*b[7];
l += 8;
b += 8;
n -= 8;
}
while(n--)
*l++ = scaledAlpha | 0x01010100*(*b++);
}
void
rgbtorgba( register unsigned short *r, register unsigned short *g,
register unsigned short *b, register unsigned int *l,
register int n, float alpha)
{
register unsigned short scaledAlpha;
scaledAlpha = (unsigned short) (alpha * 0xFF);
/* added FF for alpha so transparent textures will work - mars */
while(n>=8) {
l[0] = scaledAlpha | (b[0]<<8) | (g[0]<<16) | (r[0]<<24);
l[1] = scaledAlpha | (b[1]<<8) | (g[1]<<16) | (r[1]<<24);
l[2] = scaledAlpha | (b[2]<<8) | (g[2]<<16) | (r[2]<<24);
l[3] = scaledAlpha | (b[3]<<8) | (g[3]<<16) | (r[3]<<24);
l[4] = scaledAlpha | (b[4]<<8) | (g[4]<<16) | (r[4]<<24);
l[5] = scaledAlpha | (b[5]<<8) | (g[5]<<16) | (r[5]<<24);
l[6] = scaledAlpha | (b[6]<<8) | (g[6]<<16) | (r[6]<<24);
l[7] = scaledAlpha | (b[7]<<8) | (g[7]<<16) | (r[7]<<24);
l += 8;
r += 8;
g += 8;
b += 8;
n -= 8;
}
while(n--)
*l++ = scaledAlpha | ((*b++)<<8) | ((*g++)<<16) | ((*r++)<<24);
}
void
rgbatorgba( register unsigned short *r, register unsigned short *g,
register unsigned short *b, register unsigned short *a,
register unsigned int *l, register int n , float alpha)
{
register unsigned short scaledAlpha;
scaledAlpha = (alpha * 0xFF);
while(n>=8) {
l[0] = (unsigned short)(alpha * a[0]) | (b[0]<<8) | (g[0]<<16) | (r[0]<<24);
l[1] = (unsigned short)(alpha * a[1]) | (b[1]<<8) | (g[1]<<16) | (r[1]<<24);
l[2] = (unsigned short)(alpha * a[2]) | (b[2]<<8) | (g[2]<<16) | (r[2]<<24);
l[3] = (unsigned short)(alpha * a[3]) | (b[3]<<8) | (g[3]<<16) | (r[3]<<24);
l[4] = (unsigned short)(alpha * a[4]) | (b[4]<<8) | (g[4]<<16) | (r[4]<<24);
l[5] = (unsigned short)(alpha * a[5]) | (b[5]<<8) | (g[5]<<16) | (r[5]<<24);
l[6] = (unsigned short)(alpha * a[6]) | (b[6]<<8) | (g[6]<<16) | (r[6]<<24);
l[7] = (unsigned short)(alpha * a[7]) | (b[7]<<8) | (g[7]<<16) | (r[7]<<24);
l += 8;
r += 8;
g += 8;
b += 8;
a += 8;
n -= 8;
}
while(n--)
*l++ = ((unsigned short)(alpha * (*a++))) | ((*b++)<<8) | ((*g++)<<16) | ((*r++)<<24);
}